package main.src;

import m3u8tourl.src.com.GetAllTsUrls;
import m3u8tourl.src.com.M3u8Download;
import util.src.com.*;

import java.io.File;
import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.ExecutionException;

/**
 * @Author: Gosin
 * @Date: 2020/10/29 9:22
 */
public class TestMain {

    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2,SSLv3");

        int doneNum = 0;
        int allTsNum;
        Queue<String> tsQueue;

        String m3u8Url = "https://video.lllwo2o.com:8091/20180601/XKI7ETY799/650kb/hls/index.m3u8";
        String m3u8Path = "D:\\乱七八糟\\Downloads\\4\\";
        String m3u8Name = "index.m3u8";
        String urlHeader = m3u8Url.replaceAll("index.m3u8", "");
        String textPath = m3u8Path;
        String allUrlTextName = "a.txt";
        String errorUrlTextName = "e.txt";
        String tsPath = m3u8Path + "1\\";
        int minTsLength = 1024*1;

        int threadNum = 0;

        CreatFile a = new CreatFile(m3u8Path, tsPath);
        a.mkFiles();

        M3u8Download b = new M3u8Download(m3u8Url, m3u8Path, m3u8Name);
        File file = new File(m3u8Path);
        if (!file.exists()) {
            file.mkdir();//创建文件夹
        }

        File file1 = new File(m3u8Path+m3u8Name);
        if (!file1.exists()) {
            try {
                b.download();//得到index.m3u8文件
            } catch (Exception e) {
                System.out.println("22");
            }
        }


        GetAllTsUrls c = new GetAllTsUrls(m3u8Path, m3u8Name, textPath, allUrlTextName, urlHeader);
        c.getTsUrlText(); //得到a.txt文件,存放的是m3u8中所有的ts_url

        GetErrorTsUrls d = new GetErrorTsUrls(textPath, allUrlTextName, textPath, errorUrlTextName, "", tsPath, minTsLength);
        d.getTsUrlText(); //得到e.txt文件,存放的是所有还没下载的ts_url

        if (GetErrorTsUrls.queue.size() != 0) {

            tsQueue = GetErrorTsUrls.queue;
        } else {
            tsQueue = GetAllTsUrls.queue;
        }
        //tsQueue = GetErrorTsUrls.queue;

        if (threadNum != 0) {

            allTsNum = tsQueue.size();
            ReturnDoneNum obj = new ReturnDoneNum(doneNum);

            DownloadScheduleThread e = new DownloadScheduleThread(allTsNum, obj);
            e.start();

            CreatThreadPool f = new CreatThreadPool(tsQueue, tsPath, threadNum, obj);
            f.management();
        }

    }
}
